package com.partners.controller;

import com.partners.annotation.SimpleParam;
import com.partners.annotation.Token;
import com.partners.configure.TokenConfig;
import com.partners.model.ApiResult;
import com.partners.model.Member;
import com.partners.model.TokenCodeModel;
import com.partners.service.TokenServer;
import com.partners.utils.ApiResultUtil;
import com.partners.utils.SessionManager;
import com.partners.utils.TokenSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * @author liangyongle
 * @date 2019-01-14
 */
@RestController
@RequestMapping("token")
@Slf4j
public class TokenController {

    @Resource
    private TokenServer tokenServer;

    @Resource
    private TokenConfig tokenConfig;


     @PostMapping("createToken")
     public ApiResult createToken(@SimpleParam("uid") Integer uid,HttpServletRequest request){
         Member member =  tokenServer.createToken(uid);
         return ApiResultUtil.ok(member);
     }

//
//    @PostMapping("new")
//    public ApiResult createToken(@SimpleParam("openid") String openId, HttpServletRequest request){
//        Map<String,Object> detail = tokenServer.createToken(openId,request);
//         return ApiResultUtil.ok(detail);
//    }
//
//    @PostMapping("get")
//    public ApiResult getToken(@SimpleParam("token") String token){
//        Member member = tokenServer.getMemberByToken(token);
//        return ApiResultUtil.ok(member);
//    }
//
//   /*
//   * 生成token(缓存2)
//   * */
//    @PostMapping("create")
//    public ApiResult createAndSaveToken(@SimpleParam("openid") String openId){
//        TokenCodeModel detail = tokenServer.createAndSaveToken(openId);
//        return ApiResultUtil.ok(detail);
//    }
//
//
//    /*
//     * 获取token里面的值(缓存2)
//     * */
//    @PostMapping("getToken")
//    @Token
//    public ApiResult getCacheToken(@SimpleParam("id") Integer id){
//        String token = SessionManager.currentThreadToken();
//        Member member = new Member();
//        member.setToken(token);
//        TokenCodeModel tokens = tokenConfig.createAndSaveToken(member);
//        return ApiResultUtil.ok(tokens);
//    }
//
//
//    /**测试
//     * 获取token里面的值(缓存2)
//     * */
//    @PostMapping("cleanToken")
//    public ApiResult cleanAndSaveToken(){
//        String openId ="oR3V81baBeAiEjL6juAkYl1GuxvM";
//        Member fans = new Member();
//        fans.setId(123456);
//        fans.setName("Forever");
//        fans.setOpenId(openId);
//        String tokens = UUID.randomUUID().toString();
//        fans.setToken(tokens);
//        TokenCodeModel tokenCodeModel = tokenConfig.createAndSaveToken(fans);
//        log.info("tokens{}",tokenCodeModel);
//        return ApiResultUtil.ok(0);
//    }
//
//    @PostMapping("updateToken")
//    public ApiResult updateToken(@SimpleParam("token") String token){
//        Member fans = new Member();
//        fans.setToken(token);
//        TokenCodeModel tokenCodeModel = tokenConfig.createAndSaveToken(fans);
//        return ApiResultUtil.ok(tokenCodeModel);
//    }
//

}
